/**
 * Copyright (c), 2012~2020 iot.10086.cn All Rights Reserved
 *
 * @file tm_user.c
 * @date 2020/05/14
 * @brief
 */

/*****************************************************************************/
/* Includes                                                                  */
/*****************************************************************************/
#include "tm_data.h"
#include "tm_api.h"
#include "tm_user.h"
#include "stdio.h"
#include "stdlib.h"
#include "string.h"
/*****************************************************************************/
/* Local Definitions ( Constant and Macro )                                  */
/*****************************************************************************/
/*****************************************************************************/
/* Structures, Enum and Typedefs                                             */
/*****************************************************************************/
/*************************** Property Func List ******************************/
struct tm_prop_tbl_t tm_prop_list[] = {
  {
  .name="Temp",
  .tm_prop_rd_cb=Temp_read_cb,
  .tm_prop_wr_cb=Temp_write_cb
  },
  {
  .name="light",
  .tm_prop_rd_cb=light_read_cb,
  .tm_prop_wr_cb=light_write_cb
  } ,
  {
  .name="LED",
  .tm_prop_wr_cb=LED_write_cb
  } 
};
uint16_t tm_prop_list_size = sizeof(tm_prop_list) / sizeof(tm_prop_list[0]);
/****************************** Auto Generated *******************************/
/***************************** Service Func List *******************************/
struct tm_svc_tbl_t tm_svc_list[] = {
{"cmd",cmd_cb},
};
uint16_t tm_svc_list_size =ARRAY_SIZE(tm_svc_list);
/****************************** Auto Generated *******************************/
/*****************************************************************************/
/* Local Function Prototype                                                  */
/*****************************************************************************/
/*****************************************************************************/
/* Local Variables                                                           */
/*****************************************************************************/
extern  int  temp_value;
extern  int  light_value;
/*****************************************************************************/
/* Global Variables                                                          */
/*****************************************************************************/
/*****************************************************************************/
/* Function Implementation                                                   */
/*****************************************************************************/
/**************************** Property Func Read *****************************/
int32_t Temp_read_cb(void* res)
{
  int val=0;
  val=temp_value;
  printf("reading device temperature:%d\r\n",val);
  tm_data_struct_set_int32(res,"Temp",val);
}
int32_t light_read_cb(void* res)
{
  int val=0;
  val=light_value;
  printf("reading device illumination:%d\r\n",val);
  tm_data_struct_set_int32(res,"light",val);
}
/****************************** Auto Generated *******************************/
/**************************** Property Func Write ****************************/
int32_t Temp_write_cb(void* res)
{

  int32_t  set_value=0;
  tm_data_get_int32(res,&set_value);
  printf("receive onenet send temp value:%ld\r\n",set_value);
  tm_data_struct_set_string(res,"msg","");

}
int32_t light_write_cb(void* res)
{

  int32_t set_value=0;
  tm_data_get_int32(res,&set_value);
  printf("receive onenet send light value:%d\r\n",set_value);
  tm_data_struct_set_string(res,"msg","");

}
int32_t LED_write_cb(void* res)
{

  bool set_value=true;
  tm_data_get_bool(res,&set_value);
  printf("receive onenet send LED value:%d\r\n",set_value);
  if(set_value==true)
  {
   system("/root/leds 1 1");
  }else if(set_value==false)
  {
   system("/root/leds 0 1");
  }
  tm_data_struct_set_string(res,"msg","");

}
/****************************** Auto Generated *******************************/
/**************************** Property Func Notify ***************************/
int32_t tm_prop_bool_notify(void *data, boolean val, const int8_t *name, uint32_t timeout_ms)
{
    void *resource = NULL;
    int32_t ret = 0;    
    if(NULL == data)    
   {        
   resource = tm_data_create();    
   }else    
   {        
    resource = data;    
   }    
  tm_data_set_bool(resource, name, val, 0);    
   if(NULL == data)    
   { 
    ret = tm_post_property(resource, timeout_ms);
       
   }    
      return ret;
     tm_data_delete(resource);  
   }

int32_t tm_prop_int32_notify(void *data, int32_t val, const int8_t *name, uint32_t timeout_ms)
{
       void *resource = NULL;    
       int32_t ret = 0;    
       if(NULL == data)    
   {        
       resource = tm_data_create();    
   }else
   {        
      resource = data;    
   }  
   tm_data_set_int32(resource, name, val, 0);    
   if(NULL == data)    
   {        
     ret = tm_post_property(resource, timeout_ms);   
     
   }  
      
     return ret;
    tm_data_delete(resource); 
}
/****************************** Auto Generated *******************************/
/***************************** Event Func Notify *****************************/
/****************************** Auto Generated *******************************/
/**************************** Service Func Invoke ****************************/
int32_t cmd_cb(void* in,void* out)
{
 char *out_str="cmd success";int8_t *in_str; 
 tm_data_struct_get_string(in,"InData",&in_str);
 
 printf("cmd service invoke content:%s\n",in_str);
 if(strcmp(in_str,"alarm")==0)
 {
   system("/root/buzzer 1 0");
 }else
 {
   system("/root/buzzer 0 0");
 }
 tm_data_struct_set_string(out,"OutData",out_str);
 printf("cmd service invoke response content:%s\n",out_str);
}
/****************************** Auto Generated *******************************/

